PalaceEffect={
  scene = nil,
  effectType,
  shaderNode = nil,
  initialized = false,
  samples = {},
}
EffectType={  --行列
  EffectType_2x1=1,
  EffectType_3x1=2,
  EffectType_1x2=3,
  EffectType_2x2=4,
  EffectType_3x3=5,
}
function PalaceEffect:init(scene,type) --行列
  if(scene==nil or type==0)then
    print("PalaceEffect_error--宫格特效初始化失败")
    initialized = false
    return false
  end
  self.scene=scene
  self.effectType = type
  self.shaderNode = getMyShaderFilter(type)
  self.initialized = true
  return true
end
function getMyShaderFilter(type)
  local node = nil
  if(type==EffectType.EffectType_2x1)then
    node = KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "shaderFor2x1.frag", true)
  elseif(type==EffectType.EffectType_3x1)then
    node = KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "shaderFor3x1.frag", true)
  elseif(type==EffectType.EffectType_1x2)then
    node = KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "shaderFor1x2.frag", true)
  elseif(type==EffectType.EffectType_2x2)then
    node = KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "shaderFor2x2.frag", true)
  elseif(type==EffectType.EffectType_3x3)then
    node = KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "shaderFor3x3.frag", true)
  else
    --do things
  end
  return node
end
function PalaceEffect:getCurrentShaderNode()
  return self.shaderNode
end
function PalaceEffect:add(sample,index)

  if(sample~=nil)then
    self.samples[index] = sample
  else
    print("PalaceEffect_error--宫格特效传入的sample为空")
    local scene = self.scene
    local snapNode = addNodeAndRelease(scene,KuruSnapshotNode.create())
    sample = snapNode:getSnapshot()
    self.samples[index] = sample
  end

end
function PalaceEffect:update(sample,index)

  if(sample~=nil)then
    self.samples[index] = sample
   
  else
    print("PalaceEffect_error--宫格特效传入的sample为空")
    local scene = self.scene
    local snapNode = addNodeAndRelease(scene,KuruSnapshotNode.create())
    sample = snapNode:getSnapshot()
    self.samples[index] = sample
  end
  PalaceEffect:setSamplesWithIndex(index)
end
function PalaceEffect:commit()
  for i=1,#self.samples do
    PalaceEffect:setSamplesWithIndex(i)
  end
  addNodeAndRelease(self.scene,self.shaderNode)
end
function PalaceEffect:setSamplesWithIndex(index)
        local sample = self.samples[index]
        print("PalaceEffect:update"..index..tostring(sample))
        if(index==1)then
            self.shaderNode:setChannel0(sample)
        elseif(index==2) then
          self.shaderNode:setChannel1(sample)
        elseif(index==3) then
          self.shaderNode:setChannel2(sample)
        elseif(index==4) then
          self.shaderNode:setChannel3(sample)
        elseif(index==5) then
          self.shaderNode:getMaterial():getParameter("sample2d1"):setSampler(sample)
       --   self.shaderNode:setChannel4(sample)
        elseif(index==6) then
          self.shaderNode:getMaterial():getParameter("sample2d2"):setSampler(sample)
        --  self.shaderNode:setChannel5(sample)
        elseif(index==7) then
          self.shaderNode:getMaterial():getParameter("sample2d3"):setSampler(sample)
        --  self.shaderNode:setChannel6(sample)
        elseif(index==8) then
          self.shaderNode:getMaterial():getParameter("sample2d4"):setSampler(sample)
        elseif(index==9) then
          self.shaderNode:getMaterial():getParameter("sample2d5"):setSampler(sample)
        --  self.shaderNode:setChannel7(sample)
        else
          ------
        end
end
function PalaceEffect:isShowEnabled(enabled)
    if(self.shaderNode~=nil)then --建议都加个判断，脚本语言没有错误堆栈信息
        self.shaderNode:setEnabled(enabled)
    end
end

function addNodeAndRelease(scene, node)
  scene:addNode(node)
  node:release()
  return node
end